package com.cysion.study.ui.fragment

import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.image.RoundTransform
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.other.startActivity_ex
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.HomeNoticeAdapter
import com.cysion.study.entity.Fields
import com.cysion.study.entity.HomeNotices
import com.cysion.study.entity.Recruits
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.NoticeActivity
import com.cysion.study.ui.PositionInfoListActivity
import com.cysion.study.ui.TenancyListActivity
import com.cysion.study.widget.ShowToast
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.xiaosu.view.text.DataSetAdapter
import kotlinx.android.synthetic.main.fragment_private_home.*



//私教首页
class PrivateTeacherFragment : BaseFragment(), View.OnClickListener {


    override fun getLayoutId(): Int = R.layout.fragment_private_home
    private lateinit var homeRollingAdapter: HomeNoticeAdapter
    override fun initView() {
        tv_notice.setOnClickListener(this)
        initRefreshLayout()
//        vrl_home.rollingType = VerticalRollingLayout.BOTTOM_TOP
        //homeRollingAdapter 是继承ListAdapter 的   和ListAdapter 用法一样
//        vrl_home.setAdapter(homeRollingAdapter)
//        vrl_home.startFlipping()

        vrl_home.setOnItemClickListener { view, index ->
            context.startActivity_ex<NoticeActivity>()
        }
        tv_activity_more.setOnClickListener(this)
        tv_pro_more.setOnClickListener(this)

    }
    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setEnableLoadMore(false)
        smartLayout.setOnRefreshListener {
            getDataList()
//            smartLayout.setEnableLoadMore(true)
        }
    }



    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_notice -> {//通知列表
                context.startActivity_ex<NoticeActivity>()
            }
            R.id.tv_activity_more -> {//招聘更多
                context.startActivity_ex<PositionInfoListActivity>()
            }
            R.id.tv_pro_more -> {//场地更多
                context.startActivity_ex<TenancyListActivity>()
            }
        }
    }
    private fun getDataList() {
        Alert.loading(context)
        ApiClient.api.privateInfo(UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()

                    for (item in it.recruits) {
                        //招聘
                        addRecruits(item)

                    }
                    for (act in it.fields) {
                        addfields(act)
                    }

                    vrl_home.setDataSetAdapter(object : DataSetAdapter<HomeNotices>(it.notices) {
                        override fun text(charSequence: HomeNotices): CharSequence {
                            return charSequence.content
                        }
                    })
                    vrl_home.run()


                }
                _onError {
                    Alert.close()
                    stopRefresh()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }

    private fun addRecruits(item: Recruits) {
        val v = LayoutInflater.from(context).inflate(R.layout.flow_home_recruits, flow, false)
        var iv_img = v.findViewById<ImageView>(R.id.iv_img)
        var tv_activity_title = v.findViewById<TextView>(R.id.tv_activity_title)
        var tv_money = v.findViewById<TextView>(R.id.tv_money)
        var tv_danwei = v.findViewById<TextView>(R.id.tv_danwei)
        var tv_address = v.findViewById<TextView>(R.id.tv_address)
        Glide.with(context).load(item.images)
            .apply(RequestOptions.placeholderOf(R.mipmap.place_holder_big))
            .apply(RequestOptions.bitmapTransform(RoundTransform(6)))
            .into(iv_img)
        tv_activity_title.text=item.name
        tv_money.text=item.money
        tv_address.text=item.address
        tv_danwei.text=item.oName
        flow.addView(v)
    }

    private fun addfields(item: Fields) {
        val v = LayoutInflater.from(context).inflate(R.layout.flow_home_fields, flow_act, false)
        var iv_pro = v.findViewById<ImageView>(R.id.iv_pro)
        var tv_pro_title = v.findViewById<TextView>(R.id.tv_pro_title)
        var tv_pro_content = v.findViewById<TextView>(R.id.tv_pro_content)
        var tv_money = v.findViewById<TextView>(R.id.tv_money)
        Glide.with(context).load(item.images)
            .apply(RequestOptions.placeholderOf(R.mipmap.place_holder_big))
            .apply(RequestOptions.bitmapTransform(RoundTransform(6)))
            .into(iv_pro)
        tv_pro_title.text=item.name
        tv_pro_content.text=item.address
        tv_money.text=item.money
        flow_act.addView(v)
    }

    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }

    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }
    override fun closeMvp() {
    }

}
